<?php
namespace app\admin\controller;
use think\Db;
class Scroe extends Common{
	public function teacherindex(){
		$data = model("Scroe")->getDataAll(1);
		$this->assign('data',$data);

		$major = Db::name('major')->where('status',1)->select();
		$this->assign('major',$major);

		$subject = Db::name('subject')->where('status',1)->select();
		$this->assign('subject',$subject);

		return $this->fetch();
	}
	//选着二级联动
    public function major(){
    	$major_id = input('post.major_id');
    	$data = Db::name('subject')->where('major_id',$major_id)->select();
    	return json($data);
    } 
	
	 //软删除
    public function del(){
    	//获取的是一个数组
    	$ids = input('post.ids/a');
    	//将数组转为字符串 以逗号分隔
    	$ids = implode(',',$ids);
    	//软删除 更新状态
    	$result = Db::name('topic')->where("id in ($ids)")->setField('status',0);
    	if($result){
    		$this->success("删除成功");
    	}else{
    		$this->error("删除失败");
    	}
    }

    public function edit($id=''){
		if(request()->isPost()){
                $data = input('post.data/a');
                $scroe = Db::name('topic')->where('id',$data['id'])->field('scroe')->find();
                // dump($scroe);die;
                $scroe = $scroe['scroe'] + $data['operation_scroe'];
                $result = Db::name('topic')->where('id',$data['id'])->update(['operation_scroe'=>$data['operation_scroe'],'scroe'=>$scroe]);

                if ($result) {
                        return json(['code'=>1,'msg'=>'阅卷成功']);
                 }else{
                        return json(['code'=>0,'msg'=>'已经阅过卷啦']);
                 }

            }else{

                $scroe = model('Scroe')->editScroe($id);
                // dump($single);die;
                
                $this->assign('data',$scroe);

                return $this->fetch();
            }
    	}

	public function officeindex(){
		$data = model("Scroe")->getDataAll(1);
		$this->assign('data',$data);

		$major = Db::name('major')->where('status',1)->select();
		$this->assign('major',$major);

		$subject = Db::name('subject')->where('status',1)->select();
		$this->assign('subject',$subject);

		return $this->fetch();
	}	
	
	public function editoffice($id=''){
		if(request()->isPost()){
                $data = input('post.data/a');

                $result = Db::name('topic')->where('id',$data['id'])->update(['not_read'=>1]);

                if ($result) {
                        return json(['code'=>1,'msg'=>'审阅成功']);
                 }else{
                        return json(['code'=>0,'msg'=>'已审阅过了']);
                 }

            }else{

                $scroe = model('Scroe')->editScroe($id);
                // dump($single);die;
                
                $this->assign('data',$scroe);

                return $this->fetch();
            }
	}
}
